package junior.db.dao.mybatis.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.Map;

public class MybatisSqlUtils {

    public final static String SCRIPT_PREFIX = "<script>";
    public final static String SCRIPT_SUFFIX = "</script>";

    public String buildBySQL(Map<String, Object> params) {
        Assert.notEmpty(params, "Parameter params cannot be null or empty");
    
        Object sql = params.containsKey("sql") ? params.get("sql") :
                (params.containsKey("param1") ? params.get("param1") : params.get("arg0"));
        return joinScriptXml(sql + StringUtils.EMPTY);
    }

    private String joinScriptXml(String value) {
        StringBuilder builder = new StringBuilder();
        builder.append(SCRIPT_PREFIX).append(value).append(SCRIPT_SUFFIX);

        return builder.toString();
    }
}
